package com.fengdi.utils;


import java.util.Map;

@SuppressWarnings("all")
public class UserThreadLocalUtil{
    private static final ThreadLocal USER_THREAD_LOCAL = ThreadLocalUtil.THREAD_LOCAL;
    //根据键获取值
    public static <T> T get(){
        return (T) USER_THREAD_LOCAL.get();
    }

    //存储键值对
    public static void set(Object value){
        USER_THREAD_LOCAL.set(value);
    }

    //清除ThreadLocal 防止内存泄漏
    public static void remove(){
        USER_THREAD_LOCAL.remove();
    }

    // 获得用户的用户名
    public static String getUsername(){
        Map<String, Object> claims =  ThreadLocalUtil.get();
        return claims.get("username").toString();
    }
    // 获得用户的ID
    public static Integer  getUserID(){
        Map<String, Object> claims =  ThreadLocalUtil.get();
        return (Integer) claims.get("id");
    }
}
